package com.lark.controller;

import com.lark.pojo.Role;
import com.lark.pojo.User;
import com.lark.service.RoleService;
import com.lark.service.UserService;
import com.lark.utils.Information;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /*@RequestMapping(value = "/login")
    public String login(String username,String password,HttpSession session){
        System.out.println(userService);
        User user = userService.login(username,password);
        if(user!=null){
            session.setAttribute("user",user);
            return "";
        }
        return "";
    }*/

    @RequestMapping("/saveUI")
    public String saveUI(Model model){
        List<Role> roleList = roleService.roleList();
        model.addAttribute("roleList",roleList);
        return "/after/user-add";
    }

    @RequestMapping("/userAdd")
    public String userSave(User user,Long[] roleIds){
        if(user.getPhoto()==null){
            user.setPhoto(Information.HEAD_PHOTO);
        }
        if(user.getStatu()==null){
            user.setStatu(Information.ACCOUNT_STATE);
        }
        if(user.getRegistertime()==null){
            user.setRegistertime(Information.getTime());
        }
        userService.userSave(user,roleIds);
        return "redirect:/user/userList";
    }

    @RequestMapping("/delete/{userId}")
    public String delete(@PathVariable("userId") Long userId){
        userService.delete(userId);
        return "redirect:/user/userList";
    }

    @RequestMapping("/userList")
    public String userList(Model model){
        List<User> userList = userService.userList();
        model.addAttribute("userList",userList);
        return "/after/user-list";
    }

}
